package com.ruoyi.web.controller.module.order;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.customer.domain.CustomerUser;
import com.ruoyi.customer.service.ICustomerUserService;
import com.ruoyi.order.domain.OrderComment;
import com.ruoyi.order.service.IOrderCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 评价订单信息
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/order/comment")
public class OrderCommentController extends BaseController
{
    @Autowired
    private IOrderCommentService orderCommentService;

    @Autowired
    private ICustomerUserService customerUserService;

    /**
     * 获取评价订单列表
     */
    @PostMapping("/orderCommentList")
    public TableDataInfo list(@RequestBody OrderComment orderComment, HttpServletRequest request)
    {
        String shopId = request.getHeader("ShopId"); // 店铺id
        if(shopId != null && !shopId.equals("")){
            orderComment.setShopId(shopId);
        }
        startPageByPost();
        List<OrderComment> list = orderCommentService.selectOrderCommentList(orderComment);
        return getDataTable(list);
    }

    @Log(title = "评价订单管理 - 导出", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('order:comment:export')")
    @PostMapping("/orderCommentExport")
    public void export(HttpServletResponse response, OrderComment orderComment)
    {
        List<OrderComment> list = orderCommentService.selectOrderCommentList(orderComment);
        ExcelUtil<OrderComment> util = new ExcelUtil<OrderComment>(OrderComment.class);
        util.exportExcel(response, list, "评价订单数据");
    }

    /**
     * 根据评价订单编号获取详细信息
     */
    @GetMapping(value = { "/", "/{orderNo}" })
    public AjaxResult getInfo(@PathVariable(value = "orderNo", required = false) String orderNo)
    {
        AjaxResult ajax = AjaxResult.success();
        if (StringUtils.isNotNull(orderNo))
        {
            OrderComment orderComment = orderCommentService.selectOrderCommentByOrderNo(orderNo);
            ajax.put(AjaxResult.DATA_TAG, orderComment);
        }
        return ajax;
    }

    /**
     * 新增评价订单
     */
    @Log(title = "评价订单管理 - 新增", businessType = BusinessType.INSERT)
    @PostMapping("/orderCommentCreate")
    public AjaxResult orderCommentCreate(@Validated @RequestBody OrderComment orderComment, HttpServletRequest request)
    {
        String shopId = request.getHeader("ShopId"); // 店铺id
        if (shopId == null || shopId.equals("")) {
            return error("店铺基本配置失败，参数错误");
        }
        orderComment.setShopId(shopId);
        return toAjax(orderCommentService.insertOrderComment(orderComment));
    }

    /**
     * 修改评价订单
     */
    @PreAuthorize("@ss.hasPermi('order:comment:edit')")
    @Log(title = "评价订单管理 - 修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody OrderComment orderComment)
    {
        return toAjax(orderCommentService.updateOrderComment(orderComment));
    }

    /**
     * 删除评价订单
     */
    @PreAuthorize("@ss.hasPermi('order:comment:remove')")
    @Log(title = "评价订单管理 - 删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/{commentIds}")
    public AjaxResult remove(@PathVariable Long[] commentIds)
    {
        return toAjax(orderCommentService.deleteOrderCommentByCommentIds(commentIds));
    }

    /**
     * 状态修改
     */
    @PreAuthorize("@ss.hasPermi('order:comment:edit')")
    @Log(title = "评价订单管理 - 状态修改", businessType = BusinessType.UPDATE)
    @PutMapping("/orderCommentChangeStatus")
    public AjaxResult changeStatus(@RequestBody OrderComment orderComment)
    {
        return toAjax(orderCommentService.updateOrderCommentStatus(orderComment));
    }
}
